<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class initRbac extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'init:rbac';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('permissions')->truncate();
        DB::table('roles')->truncate();
        DB::table('user_role')->truncate();
        DB::table('role_permission')->truncate();
        $permissionManager = new \HuangYi\Rbac\Managers\PermissionManager();

//        $backend = $permissionManager->create([
//            'name' => '后台信息管理',
//            'slug' => 'backend',
//            'description' => '后台信息管理',
//        ]);

        $users = $permissionManager->create([
            'name' => '人员管理',
            'slug' => 'users',
            'description' => '人员管理',
        ]);

        $logs = $permissionManager->create([
            'name' => '操作记录',
            'slug' => 'logs',
            'description' => '操作记录',
        ]);

        $studentlog = $permissionManager->create([
            'name' => '学生操作记录',
            'slug' => 'studentlog',
            'description' => '学生操作记录',
        ]);

        $rbac = $permissionManager->create([
            'name' => '权限管理',
            'slug' => 'rbac',
            'description' => '权限管理',
        ]);

//        $StudentInformation = $permissionManager->create([
//            'name' => '学生信息',
//            'slug' => 'StudentInformation',
//            'description' => '学生信息',
//        ]);

        $students = $permissionManager->create([
            'name' => '学生管理',
            'slug' => 'students',
            'description' => '学生管理',
        ]);

        $StudentInformationConfirmation = $permissionManager->create([
            'name' => '学生信息确认信息',
            'slug' => 'StudentInformationConfirmation',
            'description' => '学生信息确认信息',
        ]);

//        $LearningPlanning = $permissionManager->create([
//            'name' => '学习企划',
//            'slug' => 'LearningPlanning',
//            'description' => '学习企划',
//        ]);

        $TermManagement = $permissionManager->create([
            'name' => '学期管理',
            'slug' => 'TermManagement',
            'description' => '学期管理',
        ]);

        $courseManagement = $permissionManager->create([
            'name' => '课程管理',
            'slug' => 'courseManagement',
            'description' => '课程管理',
        ]);

//        $QualificationCertification = $permissionManager->create([
//            'name' => '资格认证',
//            'slug' => 'QualificationCertification',
//            'description' => '资格认证',
//        ]);

        $UploadingPersonnel = $permissionManager->create([
            'name' => '可上传人员',
            'slug' => 'UploadingPersonnel',
            'description' => '可上传人员',
        ]);

        $CredentialsList = $permissionManager->create([
            'name' => '已上传资格证书列表',
            'slug' => 'CredentialsList',
            'description' => '已上传资格证书列表',
        ]);


//        $ExaminationPlanning = $permissionManager->create([
//            'name' => '考试规划',
//            'slug' => 'ExaminationPlanning',
//            'description' => '考试规划',
//        ]);

        $ExaminationManagement = $permissionManager->create([
            'name' => '考试管理',
            'slug' => 'ExaminationManagement',
            'description' => '考试管理',
        ]);

        $PerformanceManagement = $permissionManager->create([
            'name' => '成绩管理',
            'slug' => 'PerformanceManagement',
            'description' => '成绩管理',
        ]);

        $EnglishManagement = $permissionManager->create([
            'name' => '四六级管理',
            'slug' => 'EnglishManagement',
            'description' => '四六级管理',
        ]);

//        $CourseArrangement = $permissionManager->create([
//            'name' => '课程安排',
//            'slug' => 'CourseArrangement',
//            'description' => '课程安排',
//        ]);

        $ElectiveStudents = $permissionManager->create([
            'name' => '选课学员',
            'slug' => 'ElectiveStudents',
            'description' => '选课学员',
        ]);

        $ClassManagement = $permissionManager->create([
            'name' => '课组管理',
            'slug' => 'ClassManagement',
            'description' => '课组管理',
        ]);

        $ClassSelectionManagement = $permissionManager->create([
            'name' => '选课管理',
            'slug' => 'ClassSelectionManagement',
            'description' => '选课管理',
        ]);
        $roleManager = new \HuangYi\Rbac\Managers\RoleManager();

        $admin = $roleManager->create([
            'name' => '管理员',
            'slug' => 'admin',
            'description' => '管理员',
        ]);

        $admin->attachPermissions(
            [
                 $users->id, $studentlog->id, $logs->id, $PerformanceManagement->id,
                $ClassManagement->id,
                $students->id, $TermManagement->id,
                $courseManagement->id,  $rbac->id,
                $UploadingPersonnel->id, $CredentialsList->id,
                $ExaminationManagement->id, $EnglishManagement->id,
                $ElectiveStudents->id, $ClassSelectionManagement->id
            ]
        );
        $user = User::find(1);

// 绑定一个角色
        $user->attachRoles($admin->id);

    }
}
